package com.cloudkinto.service.covershows.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.dao.NotificationDao;
import com.cloudkinto.entity.CustomerCaseDo;
import com.cloudkinto.entity.NotificationDo;
import com.cloudkinto.service.covershows.ThymeleafShowNoticeService;
import com.cloudkinto.service.statistics.vo.NotificationPageRes;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/3/25 15:20
 **/
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ThymeleafShowNoticeServiceImpl extends ServiceImpl<NotificationDao, NotificationDo> implements ThymeleafShowNoticeService {
    @Override
    public List<NotificationPageRes> getListPage(Map<String, String> map) {
        List<NotificationPageRes> list = new ArrayList<>();
        IPage<NotificationDo> page = pageInit(map);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        long pages = page.getPages();
        List<NotificationDo> notificationDos = page.getRecords();
        notificationDos.stream().forEach(notificationDo -> {
            NotificationPageRes notificationPageRes = new NotificationPageRes();
            BeanUtils.copyProperties(notificationDo, notificationPageRes);
            //将无需字段存入页数
            notificationPageRes.setIsRead(Integer.valueOf(pages+""));
            notificationPageRes.setContent(notificationDo.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "")
                    .replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "")
                    .replaceAll("</[a-zA-Z]+[1-9]?>", ""));
            list.add(notificationPageRes);
        });
        return list;
    }

    private IPage<NotificationDo> pageInit(Map<String, String> map) {
        IPage<NotificationDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 5);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }
}
